#include <QGraphicsScene>
#include <QPainter>

#include "PageItemsView.h"
#include "AppIconView.h"

#define APP_ICON_PER_PAGE    (14)
#define APP_ICON_HEIGHT     (100)
#define APP_ICON_WIDTH      (100)

static void fillScreenItems (const QList<AppIconView *>& itemList, 
                             qreal w, qreal h,
                             qreal itemWidth, qreal itemHeight,
                             quint8 nHItems, quint8 nVItems)
{
    qreal vSpace = ((h - (nVItems * itemHeight)) / nVItems);
    qreal hSpace = ((w - (nHItems * itemWidth)) / nHItems);

    vSpace = qMin(((itemHeight / nVItems) / 2), vSpace);
    hSpace = qMin(((itemWidth / nHItems) / 2), hSpace);

    qreal x = hSpace;
    qreal y = vSpace;
    quint8 n = 0;
    foreach (AppIconView *item, itemList) {
        item->setPos(x, y);
        if (++n == nHItems) {
            y += vSpace + itemHeight;
            x = hSpace;
            n = 0;
        } else {
            x += hSpace + itemWidth;
        }
    }
}

PageItemsView::PageItemsView (QGraphicsItem *parent)
    : UIView(parent)
{
    m_currentPage = 0;
}

PageItemsView::~PageItemsView() {
    QList<AppIconView *>::iterator it = m_appList.begin();
    while (it != m_appList.end()) {
        delete *it;
        it = m_appList.erase(it);
    }
}

void PageItemsView::addIconView (AppIconView *iconView) {
    iconView->setSize(APP_ICON_WIDTH, APP_ICON_HEIGHT);
    if (m_appList.size() < APP_ICON_PER_PAGE)
        scene()->addItem(iconView);
    m_appList.append(iconView);
}

void PageItemsView::setCurrentPage (quint8 currentPage) {
    QList<AppIconView *> items;

    setUpdatesEnabled(false);

    items = m_appList.mid(m_currentPage * APP_ICON_PER_PAGE, APP_ICON_PER_PAGE);   
    foreach (AppIconView *item, items) {
        if (item->scene() != NULL)
            item->scene()->removeItem(item);
    }

    m_currentPage = currentPage;
    items = m_appList.mid(m_currentPage * APP_ICON_PER_PAGE, APP_ICON_PER_PAGE);   
    foreach (AppIconView *item, items)
        scene()->addItem(item);

    changeOrientation(UIDevice::currentDevice()->orientationIsLandscape());
    setUpdatesEnabled(true);
    update(boundingRect());
}

void PageItemsView::changeOrientation (bool isLandscape) {
    QList<AppIconView *> items = m_appList.mid(m_currentPage * APP_ICON_PER_PAGE, 
                                               APP_ICON_PER_PAGE);
    if (isLandscape) {
        fillScreenItems(items, width(), height(), 
                        APP_ICON_WIDTH, APP_ICON_HEIGHT, 6, 3);
    } else {
        fillScreenItems(items, width(), height(), 
                        APP_ICON_WIDTH, APP_ICON_HEIGHT, 4, 4);
    }
}

void PageItemsView::drawRect (QPainter *painter, const QRect& rect) {
    int nrPages = (m_appList.size() / APP_ICON_PER_PAGE);
    if (m_appList.size() % APP_ICON_PER_PAGE) nrPages++;

	int x = (rect.width() - (nrPages * 16)) / 2;
	int y = rect.height() - 12;

    for (int i = 0; i < nrPages; ++i) {
        if (i == m_currentPage)
            painter->setBrush(Qt::white);
        else
            painter->setBrush(QColor(0x5f, 0x60, 0x60));

        painter->setPen(Qt::NoPen);
        painter->drawEllipse(x + (i * 16), y, 6, 6);
    }
}

void PageItemsView::swipeEvent (UITouch *touch) {
    qreal dx = touch->deltaX();

    if (dx < 0) {
        int nrPages = (m_appList.size() / APP_ICON_PER_PAGE);
        if (m_appList.size() % APP_ICON_PER_PAGE) nrPages++;

        if ((m_currentPage + 1) < nrPages)
            setCurrentPage(m_currentPage + 1);
    } else if (m_currentPage > 0) {
        setCurrentPage(m_currentPage - 1);
    }
}

